/*
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#ifndef DEV_PLDM_OVER_MCTP_EMULEX_H
#define DEV_PLDM_OVER_MCTP_EMULEX_H

#include <pldm_over_mctp/pldm_over_mctp_standard.h>
#include <cstdint>
#include <vector>
#include <string>
#include <map>

#include <pldm/pldm_emulex.h>

namespace dev {

typedef struct fru_tlv_entry {
    uint8_t type;
    uint8_t length;
    std::string value;
} fru_tlv_entry_t;

typedef struct fru_record_info {
    uint16_t record_id;
    uint8_t record_type;
    uint8_t number_field;
    uint8_t encoding_type;
    std::vector<fru_tlv_entry_t> tlv_entries;
} fru_record_info_t;

typedef struct emulex_fru_info {
    std::string firmware_version;
    std::string part_number;
    std::string serial_number;
    std::string port0_wwnn;
    std::string port1_wwnn;
    std::string port0_wwpn;
    std::string port1_wwpn;
} emulex_fru_info_t;

static const std::map<uint8_t, uint8_t> record_tlv_num_emulex = {
    {FRU_RECORD_ID_GENERAL, 14},
    {FRU_RECORD_ID_CHIP, 3},
    {FRU_RECORD_ID_PORT0, 8},
    {FRU_RECORD_ID_PORT1, 8}
};

class MC_API pldm_over_mctp_emulex : public pldm_over_mctp_standard {
public:
    pldm_over_mctp_emulex(mctp& mctp_obj) : pldm_over_mctp_standard(mctp_obj){
    };
    ~pldm_over_mctp_emulex() {
    };

    bool update_all_fru_records();
    bool get_emulex_fru_info(emulex_fru_info_t* fru_info);
    bool get_chip_temperature(int16_t* temperature);
    bool get_link_status(uint8_t port_id, uint8_t* status);
    bool get_link_speed(uint8_t port_id, uint32_t* speed);

private:
    bool send_fru_single_request(uint8_t transfer_operation_flag, uint32_t data_transfer_handle,
                                std::vector<fru_tlv_entry_t>& tlv_entries, uint32_t* next_handle, uint8_t* transfer_flag,uint16_t* response_record_id);
    bool fru_response_is_valid(const pldm_get_fru_record_table_rsp_t* rsp, size_t rsp_len);
    void fill_fru_table(const pldm_get_fru_record_table_rsp_t* rsp, size_t rsp_len, 
                       std::vector<fru_tlv_entry_t>& tlv_entries);
    bool send_fru_request(uint16_t record_id, std::vector<fru_tlv_entry_t>& tlv_entries);
    
    
    std::map<uint16_t, std::vector<fru_tlv_entry_t>> fru_table_cache_;
    std::mutex cache_mutex_;
    bool cache_initialized_ = false;
    std::string format_wwn(const std::string& input_str, const std::string& prefix_type);
    
};

} // namespace dev

#endif // DEV_PLDM_OVER_MCTP_EMULEX_H
